package com.jxmcloud.business.cut.service.impl.logic;

import com.google.common.collect.Lists;
import com.jxmcloud.business.ce.service.CeSysParameterService;
import com.jxmcloud.business.cut.dtoCommon.AccountInfoDto;
import com.jxmcloud.business.cut.dtoCommon.FlowInfoDto;
import com.jxmcloud.business.cut.dtoCommon.InviteSuccessListDto;
import com.jxmcloud.business.cut.model.database.DBUserInviteSuccess;
import com.jxmcloud.business.cut.service.database.IUserInviteSuccessDBService;
import com.jxmcloud.business.cut.service.logic.IPackMessageToClient;
import com.jxmcloud.business.cut.service.user.IUserActivityService;
import com.jxmcloud.business.cut.service.user.IUserBagService;
import com.jxmcloud.business.cut.service.user.IUserCashDetailListService;
import com.jxmcloud.business.cut.service.user.IUserGiftService;
import com.jxmcloud.business.cut.util.MyTool;
import com.jxmcloud.business.cut.vo.client.LoginFromXLVo;
import com.jxmcloud.business.cut.vo.user.PlayerInfoVo;
import com.jxmcloud.business.cut.vo.user.UserActivityVo;
import com.jxmcloud.business.cut.vo.user.UserBaseVo;
import com.jxmcloud.business.cut.vo.user.UserFlowVo;
import com.xlcloud.platform.util.DateUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;


// 客户端逻辑汇总
@Service
@Slf4j
public class PackMessageToClientImpl implements IPackMessageToClient {
    @Resource
    private IUserInviteSuccessDBService inviteSuccessDBService;
    @Resource
    private IUserBagService userBagService;
    @Resource
    private IUserCashDetailListService userCashDetailListService;
    @Autowired
    private CeSysParameterService sysParameterService;
    @Resource
    private IUserGiftService userGiftService;
    @Resource
    private IUserActivityService userActivityService;

    @Override
    public LoginFromXLVo packPlayerLoginData(PlayerInfoVo player) {
        LoginFromXLVo loginDto = new LoginFromXLVo();
        UserBaseVo base = player.getBase();
        UserActivityVo activity = player.getActivity();
        loginDto.setUid(base.getUid());
        loginDto.setSession(base.getSession());
        loginDto.setTime(MyTool.getCurrentTimeSecond(null));

        // 账号信息
        AccountInfoDto accountInfo = new AccountInfoDto();
        accountInfo.setXlId(base.getXlId().intValue());
        accountInfo.setAccount(base.getAccount());
        accountInfo.setIcon(base.getIcon());
        accountInfo.setNickName(base.getNickName());
        accountInfo.setCreateTime(base.getCreateTime());
        loginDto.setAccountInfo(accountInfo);

        // 邀请列表
        List<DBUserInviteSuccess> listData = inviteSuccessDBService.selectAll(base.getUid(), activity.getActivityId());
        List<InviteSuccessListDto> inviteSuccessList = Lists.newArrayList();
        for (DBUserInviteSuccess listDatum : listData) {
            InviteSuccessListDto inviteSuccessListDto = new InviteSuccessListDto();
            inviteSuccessListDto.setXlId(listDatum.getInvitedXlId().intValue());
            inviteSuccessListDto.setAccount(listDatum.getInvitedAccount());
            inviteSuccessListDto.setName(listDatum.getInvitedName());
            inviteSuccessListDto.setIcon(listDatum.getInvitedIcon());
            Long respTime = DateUtil.dateToTime(listDatum.getRespTime());
            inviteSuccessListDto.setTime(respTime.intValue());
            inviteSuccessList.add(inviteSuccessListDto);
        }
        loginDto.setInviteSuccessList(inviteSuccessList);

        // 流程信息
        UserFlowVo flow = player.getFlowInfo();
        FlowInfoDto flowInfo = new FlowInfoDto();
        flowInfo.setStartId(activity.getStartId());
        flowInfo.setStepId(flow.getStepId());
        loginDto.setFlowInfo(flowInfo);

        // 背包数据填充
        loginDto.setBagInfo(userBagService.packBagData(player));
        // 零钱记录
        loginDto.setCashDetailList(userCashDetailListService.packPlayer(player));
        // 加速礼包是否购买
        loginDto.setHasBuySpeedGift(userGiftService.isBuySpeedGift(player));

        // 上次提现时间戳s, 取用活动创建时间
        loginDto.setLastMoneyTime(activity.getCreateTime());

        // 通知列表
        Integer[] sendClientNoticeType = userActivityService.getSendClientNoticeType(player);
        loginDto.setServerNoticeList(sendClientNoticeType);

        return loginDto;
    }
}
